package ss.pku.jsa.type.inference;

public class ArrayQType extends ObjectQType {
	// private static final int kind = 6;
	private FieldKey elementKey;

	public ArrayQType(RefQType element, Location l, String name,
			boolean original) {
		super(TypeKey.getKeyForArrayOf(element.type), l, name, original);
		setElement(element);
	}

	public int getKind() {
		return 6;
	}

	public boolean isArray() {
		return true;
	}

	public String toString() {
		StringBuffer s = new StringBuffer();
		s.append(getName() + ": " + this.q + " array of " + getElement());
		return s.toString();
	}

	public String getName() {
		return this.name;
	}

	public QType copy(Location l, String name) {
		RefQType origElement = getElement();
		Profile.logMemStart("ArrayQType.copy");
		ArrayQType aqt = new ArrayQType(origElement.copy(l, origElement.name),
				l, name, false);
		Profile.logMemEnd();
		return aqt;
	}

	private void setElement(RefQType element) {
		this.elementKey = ArrayFieldKey.getInstance(element);

		element.setKey(this.elementKey);
		addFieldQType(this.elementKey, element);
	}

	public RefQType getElement() {
		RefQType ret = lookupFieldQType(this.elementKey, false);

		return ret;
	}

	public int getSize() {
		if (getElement() != null) {
			return getElement().getSize();
		}
		return 1;
	}
}
